package func

import java.io.File

/**
 *
 * @author tzp
 * @since 2021/1/16
 */
object 真FilesMatches {
  def filesHere = (new File(".")).listFiles

  def fileMatching(query: String, matcher: (String, String) => Boolean) = {
    for (file <- filesHere if matcher(file.getName, query)) yield file
  }

  def fileEndsWith(query: String) = {
    fileMatching(query, _.endsWith(_))
  }

  def fileContains(query: String) = {
    fileMatching(query, _.contains(_))
  }

  //把query参数闭包进去的写法
  def fileMatching(matcher: String => Boolean) = {
    for (file <- filesHere if matcher(file.getName)) yield file
  }

  def fileRegex(query: String) = {
    fileMatching(_.matches(query))
  }

  def main(args: Array[String]): Unit = {
    println(fileEndsWith("scala").mkString)
    println(fileContains("o").mkString)
    println(fileRegex("o.*").mkString)
  }
}
